/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.page;

import com.cleangwt.bootstrap.client.style.Btn;
import com.cleangwt.bootstrap.client.ui.Alert;
import com.cleangwt.bootstrap.client.ui.GroupFormPanel;
import com.cleangwt.bootstrap.support.client.TooltipErrorHandler;
import com.cleangwt.page.client.PageContext.CodeSplit;
import com.cleangwt.page.client.PageContext.Page;
import com.cleangwt.page.client.PageContext.Prefetch;
import com.cleangwt.showcase.client.component.ShowcasePage;
import com.cleangwt.showcase.client.resource.PageGroups;
import com.cleangwt.showcase.client.resource.i18n.ShowcaseMessages;
import com.cleangwt.showcase.client.resource.i18n.ValidatorMessages;
import com.cleangwt.uibinder.client.RequiresFormRebind;
import com.cleangwt.uibinder.client.UiInput;
import com.cleangwt.user.client.ui.Button;
import com.cleangwt.user.client.ui.DateBox;
import com.cleangwt.user.client.ui.Lay;
import com.cleangwt.user.client.ui.TextBox;
import com.cleangwt.validation.client.Validator;
import com.cleangwt.validation.client.constraints.Future;
import com.cleangwt.validation.client.constraints.NotNull;
import com.cleangwt.validation.client.constraints.Past;
import com.cleangwt.validation.client.constraints.Pattern;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
@Prefetch(PageGroups.VALIDATION)
@CodeSplit
@Page
public abstract class BaseValidationDemo extends ShowcasePage implements RequiresFormRebind {

  @UiInput
  @NotNull
  TextBox txtf1 = new TextBox();

  @UiInput
  @Pattern(regexp = "[A-Z]+")
  TextBox txtf2 = new TextBox();

  @UiInput
  @Future
  DateBox txtfFutrue = new DateBox();

  @UiInput
  @Past
  DateBox txtfPast = new DateBox();

  public BaseValidationDemo() {
    super(ShowcaseMessages.Impl.basevalidation(),
        "Set client field constraints using base constraint annotaions.", "BaseValidationDemo.java");
    final ValidatorMessages c = GWT.create(ValidatorMessages.class);
    GroupFormPanel form = new GroupFormPanel();
    Button btnSubmit = new Button(c.verify());
    final Validator validator = new Validator(new TooltipErrorHandler());

    // layout
    setContentWidget(form);
    form.buildVertical().add("Required Validator", txtf1).div(6).add("Match Validator", txtf2).div(
        6).add("Future Validator", txtfFutrue).div(6).add("Past Validator", txtfPast).div(6);

    form.add(Lay.pullRight(Btn.primary(btnSubmit)));

    this.addConstraints(validator);
    validator.activate();
    // actions
    btnSubmit.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        if (validator.validate()) {
          Alert.alert(c.valid());
        }
      }
    });
  }
}
